import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import { routerpop } from '../src/router/index';
import { useState, useEffect } from 'react';
import { extractPathsAndElements } from '../src/hook/routerHook';
import { getStorage } from './store';
import { getMenu } from '../src/api/system/router'
function App() {
  const [appRouter, setAppRouter] = useState(routerpop);
  useEffect(() => {
    // 模拟延迟加载
    const interval = setInterval(()=>{
      console.log(111)
      if(getStorage('token')){
        getMenu().then((res) => {
          console.log(res)
          if(res.data.code === 200){
            const treelist = res.data.data
            const updatedRouter = routerpop.map((route) => {
              if (route.path === '/admin') {
                return {
                  ...route,
                  children: extractPathsAndElements(treelist),
                };
              }
              return route;
            });
            console.log(updatedRouter)
            setAppRouter(updatedRouter);
            clearInterval(interval);
          }
        })
      }
    },500)
    return () => clearInterval(interval);
  }, []);
  const browserRouter = createBrowserRouter(appRouter);
  return (
    <>
      <RouterProvider router={browserRouter}>
          {/* 这里放置你的应用程序内容 */}
        </RouterProvider>
    </>
  );
}

export default App;
